// pages/orderSheet/orderDetails.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath: "/images",
    order_sn:'',
    apply_no:'',
    orderItems:[],
    //收货地址
   shipAddress:{},
   shipAddress1:{addressId:'1',consignee:'许先生',mobile:'13060695000',address:'广东省 广州市 番禺区 汉溪大道东362号粤海广场写字楼38层3807室',isDefault:true},
    //价钱合计
    totalPrice: 0,
    //订单详情
    detail: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let order_sn = options.order_sn;

    let apply_no = options.apply_no;
     
    const util = require("../../utils/util")
    this.setData({
      order_sn,
      apply_no,
      imgPath: util.getImgPath()
    })
    wx.setNavigationBarTitle({
      title: '订单详细',
    })
     //计算总价
     this.calTotalPrice()

  },
    /**
   * 复制信息
   */
  copyInfo(no) {
    let order_no = no.currentTarget.dataset.no
    wx.setClipboardData({
      data: order_no,
      success(res) {
      //  console.log("res", res)
      }
    })
  },
         /**
     * 计算总价
     */
    calTotalPrice() {
      let num = 0.0;
      for (let item in this.data.orderItems) {
        num += Number(item.price);
      }
      this.setData({
        totalPrice:num.toFixed(2)
      })

    },
    shouhou(){
      wx.navigateTo({
        url: '/pages/orderSheet/applyAfterSale?order_sn='+this.data.order_sn,
      })
    },
    wuliu(){
      wx.navigateTo({
        url: '/pages/orderSheet/viewLogistics?order_sn='+this.data.order_sn,
      })
    },

     /**
   * 确认收货
   * @param {*} e 
   */
  confirmReceipt(e) {
    
    const that = this;

    //订单号
    let order_sn = e.currentTarget.dataset.orderno;
    wx.showModal({
      title: '确认收货',
      content: '确认已收到货？',
      complete: (res) => {
        //取消
        if (res.cancel) {

        }
        //确定
        if (res.confirm) {
          const api = require("../../api/api")
          api.user.orderConfirm({
            order_sn: order_sn,
            success(res) {
              // console.log("orderConfirm,res", res)
              
              wx.navigateTo({
                url: '/pages/orderSheet/myOrderSheet',
              })
            }
          })
        }
      }
    })
  },
  contact() {
    // wx.navigateTo({
    //   url: '/pages/customerService/index',
    // })
    //写入回调地址
    wx.setStorageSync('im:redirectUrl', "/pages/orderSheet/orderDetails?order_sn="+this.data.order_sn);
    wx.navigateTo({
      url: '/im/pages/index?type=kefu',
    })
  },
    /**
   * 立即支付
   */
   /**
   * 立即支付
   * @param {*} e 
   */
  payNow(e) {
    // console.log("payNow", e)
    let order_sn = this.data.order_sn;
    const api = require("../../api/api");
    api.order.payment({
      order_sn: order_sn,
      pay_type: 'wx',
      pay_client: "lite",
      success(resPayment) {
        // console.log("payment,resPayment", resPayment)

        if (resPayment.data.code != 0) {
          wx.showToast({
            title: resPayment.data.msg,
            icon: 'none'
          })
          return;
        }

        let dataPayment = resPayment.data.data;

        //如果data=="success"，不再发起支付
        if (dataPayment == "success") {
          wx.showToast({
            title: '订单已完成',
          })

          wx.navigateTo({
            url: '/pages/orderSheet/myOrderSheet',
          })

          return;
        }

        //微信支付
        wx.requestPayment({
          timeStamp: dataPayment.timeStamp,
          nonceStr: dataPayment.nonceStr,
          package: dataPayment.package,
          signType: 'MD5',
          paySign: dataPayment.paySign,
          success(resPay) {
            // console.log("wx.requestPayment,resPay", resPay)

            wx.navigateTo({
              url: '/pages/orderSheet/paySuccess',
            })
          },
          fail(resPay) {
            // console.log("wx.requestPayment,resPay", resPay)

            // wx.navigateTo({
            //   url: '/pages/orderSheet/payFail',
            // })
            wx.showToast({
              title: '支付失败',
              icon:'error'
            })

            //wx.navigateBack();
          }
        })
      }
    })

    // console.log("this.data.orderList",this.data.orderList);
    // let order=null;
    // for(let orderTemp of this.data.orderList){
    //   if(orderTemp.order_sn==order_sn){
    //     order=orderTemp;
    //   }
    // }
    // if(order==null){
    //   wx.showToast({
    //     title: '订单不存在',
    //     icon:'none'
    //   })
    //   return;
    // }
    // console.log("order",order)
    // return;

    // //设置订单数据
    // this.setOrderData(order_sn);

    // //订单支付
    // wx.navigateTo({
    //   url: '/pages/orderSheet/orderSetling',
    // })
  },
  confirmShop(){
          //设置tabbar激活项
          const util = require("../../utils/util")
          util.setTabbarActive('service');
          wx.redirectTo({
            url: '/pages/home/index',
          })
  },
   // 撤回申请
  confirmrew(){
    let that = this;
    let order_sn = this.data.order_sn;
    wx.showModal({
      title: '确认撤销',
      content: '确认撤销？',
      complete: (res) => {
        //取消
        if (res.cancel) {

        }
        //确定
        if (res.confirm) {
          const api = require("../../api/api")
          api.afterSale.del({
           // logistics_nu: order_sn,
            apply_no: that.data.apply_no,
            success(res) {
              // that.setData({
              //   orderItems: orderItems,
              //   detail: data.data,
              //   shipAddress: data.data.orders_logistics
              // })
              
              if(res.data.code == 0){
   
                wx.showToast({
                  title: '撤销成功',
                })   

                wx.navigateTo({
                  url: '/pages/orderSheet/myOrderSheet',
                })
              }
            }
          })
        }
      }
    })
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   // console.log('this.data.order_sn',this.data.order_sn);
    const api = require("../../api/api")
    let that = this;
    let order_sn = this.data.order_sn
    api.order.getorderDetail({
      order_sn: order_sn,
      success(res) {
       let data = res.data;  
       if(data.code == 0){
              //订单项
              let orderItems = [];
         //    console.log('data.data.data',data);
          //   console.log('data.data.goods_info',data.data.goods_info);
              for (const item of data.data.goods_info) {
                orderItems.push({
                  itemId: item.goods_id,
                  itemNum: item.goods_type==300?item.buy_number/4:item.buy_number,
                  itemImgSrc: item.image,
                  itemName: item.goods_name,
                  itemDescrition: item.stock_info.spec_value_name,
                  itemPrice: item.price,
                })
              }

            //  console.log('data.data.orders_logistics',data.data.orders_logistics);
            //  console.log('data.data.orders_logistics',Object.keys(data.data.orders_logistics).length>0);
             
              that.setData({
                orderItems: orderItems,
                detail: data.data,
                shipAddress: data.data.orders_logistics,
              })

       }
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})